public class KAASMResourceAjustmentController
{
     public Activity_Information__c ActivityInfo{get;set;}
     public CommonAttribute CommonAtt{get;set;}
     public String OutletName{get;set;}
     public CommonMethod getCommonMethod;
     public CommonRecursionMethod getRecursionMethod;
     public List<String> ColumnETList{get;set;}
     public  List<DynamicColumnEntry> ETDynamicColumnEntryList{get;set;}
     public  List<DynamicColumnEntry> FGDynamicColumnEntryList{get;set;}
      
     public List<Activity_Competition_Brand_Detail__c> ETCompetitionList{get;set;}
     public List<String> ColumnFGList{get;set;}
      public Participator_Detail_Area__c parDetailArea;
     public List<SelectOption> ParticipatorKAList{get;set;}//参与列表中的KA

     List<Resource_Allocation_Detail__c> ETResourceDetailList;
     List<Resource_Allocation_Detail__c> FGResourceDetailList;
     
     public Boolean IsDisplayForSubmit{get;set;}
     public Boolean IsDisplayForSave{get;set;}
     public Boolean IsDisplayForComfirmDate{get;set;}
     public Boolean IsKAOrRegionActivity{get;set;}//进入该页面的活动类型是KA还是Region
     public string SubordinateUserId{get;set;}//获取当前用户下面的所有用户
     public List<id> SubordinateUserIdList{get;set;}//获取当前用户下面的所有用户
     public List<SelectOption> CityList{get;set;}//城市列表
   //  public List<KAMConfirmASMDate__c> kamConfirmAsmDateList{get;set;}
     public Integer outletNumber{get;set;}
     public User user
     {
         get{return [select id,UserRegion__c from User where id=:UserInfo.getUserId()];}
     }
     public String RecordId
     {
           get
           {
               return String.valueOf(System.currentPageReference().getParameters().get('id')).substring(0,15);  
           }
           set;
     }
     //活力类型
        public String activityType
     {
           get
           {
               return String.valueOf(System.currentPageReference().getParameters().get('activityType'));  
           //   return 'KA';
           }
           set;
     }
     //加载FG资源 
     public List<AllocationResourceEntry> FGResourceList
     {
         get{
                if(ActivityInfo.Activity_Type__c!='Region(剩余资源)')
                    return ETAndFGResourceList('FG');
                 else
                    return null;
              }
     }
      //加载ET资源 
     public List<AllocationResourceEntry> ETResourceList
     {
         get{return ETAndFGResourceList('ET');}
     }
       //区域占比
      public  List<DynamicColumnEntry> TotalTargetList{
        get;
        set;}
   public KAASMResourceAjustmentController (ApexPages.StandardController controller)
     {
         getCommonMethod=new CommonMethod();
         CommonAtt=new CommonAttribute ();
         getRecursionMethod=new CommonRecursionMethod();
         
         if(CityList==NULL)
             CityList=new List<SelectOption>();
         else
             CityList.clear();
         
         if(ParticipatorKAList==NULL)
             ParticipatorKAList=new List<SelectOption>();
         else
             ParticipatorKAList.clear();
             
                 //获取当前用户下的子用户SubordinateUserId 
             SubordinateUserIdList=new List<id>(getRecursionMethod.getASMBelowRoleId());
             SubordinateUserId ='';
               for(Id idString:SubordinateUserIdList)
               {
                   if(SubordinateUserId=='')
                       SubordinateUserId='\''+idString+'\'';
                    else
                    {
                        SubordinateUserId+=',';
                        SubordinateUserId+='\''+idString+'\'';
                    }
               }
      
         map<string,List<SelectOption>> KAAndCityList=getCommonMethod.getSubordinateCity(RecordId,SubordinateUserIdList);

         if(KAAndCityList!=NULL)
         {
             if(KAAndCityList.get('City')!=NULL)
                 CityList=KAAndCityList.get('City');
             if(KAAndCityList.get('KA')!=NULL)
                 ParticipatorKAList=KAAndCityList.get('KA');
         }
        
          IsKAOrRegionActivity=true;
         if(activityType=='Region')
            IsKAOrRegionActivity=false;
         IsDisplayForSave=false;
         IsDisplayForSubmit=false;
         IsDisplayForComfirmDate=false;
         
             ActivityInfo=[Select Id,Name,IsNcOrNotForOT__c,IsNcOrNotForKA__c,Activity_End_Date__c, Activity_Notice__c, Activity_Stage__c, Activity_Start_Date__c, 
                               IsNcOrNotForRegion__c,Activity_Type_State__c, Activity_Type__c,Serial_Number__c,Regional_Activity_Type__c 
                               FROM Activity_Information__c where id=: RecordId]; 
         
         //查出生效的审批记录
             List<Participator_Detail_Area__c> parDetailAreaList=[select id,Submit_ASM__c,Outlet_Amount__c,Approve_Type__c,Approve_Description__c 
                                                                                                    from Participator_Detail_Area__c where Activity_Information__c=:RecordId  and 
                                                                                                    Submit_ASM__c=:UserInfo.getUserId()  and IsActive__c=true limit 1];
             if(parDetailAreaList!=NULL && parDetailAreaList.size()>0)
                 parDetailArea=parDetailAreaList[0];  
             
           outletNumber=[select count() from Participator_Detail_Outlet__c where ActivityId__c=:RecordId and OwnerId in:SubordinateUserIdList];     
          //查出该ASM是否已被KAM确认时间
          /*
          kamConfirmAsmDateList=new List<KAMConfirmASMDate__c> ();
          if(parDetailArea!=null){
          kamConfirmAsmDateList=[select id,ConfirmKAM__c,KAList__c,Participator_Detail_Area__c,Participator_Detail_Area__r.id from KAMConfirmASMDate__c 
                                                                                                            where IsConfirm__c=true and Participator_Detail_Area__r.Submit_ASM__r.id=:user.id and Participator_Detail_Area__r.id=:parDetailArea.id and
                                                                                                            Participator_Detail_Area__r.Activity_Information__c=:RecordId];
      }*/
              if(ActivityInfo.Activity_Stage__c=='活动已取消')
             {
                  IsDisplayForSubmit=false;
                   IsDisplayForSave=false;
                   IsDisplayForComfirmDate=false;
             }  
             else
             {
                  Date StartDT=ActivityInfo.Activity_Start_Date__c;                   
                  if(Date.today()>= StartDT.addDays(-21))//开始活动前21天不可以调时间
                  {   
                       IsDisplayForSubmit=false;
                       IsDisplayForSave=false;
                       IsDisplayForComfirmDate=false;
                   }      
                   else
                   {                     
                        if(parDetailArea != NULL)
                        {
                         if(ActivityInfo.Activity_Type__c=='Region' || ActivityInfo.Activity_Type__c=='Region(剩余资源)'){
                            if(parDetailArea.Approve_Type__c=='SRTM已审批' && parDetailArea.Approve_Description__c=='同意')
                            {
                                IsDisplayForSubmit=true;
                                 IsDisplayForComfirmDate=true;
                                 IsDisplayForSave=true;
                             }
                             if( parDetailArea.Approve_Description__c=='拒绝')
                              {
                                  IsDisplayForSubmit=true;
                                 IsDisplayForComfirmDate=false;
                                 IsDisplayForSave=true;
                             }
                            }
                            
                            else{
                             if(parDetailArea.Approve_Type__c=='ASM已提交')
                                IsDisplayForSubmit=true;                         
                                IsDisplayForSave=true;          
                                if(ActivityInfo.Activity_Type__c=='KA')
                                      IsDisplayForComfirmDate=true;      
                            }
                            if(parDetailArea.Approve_Type__c=='ASM已确认时间')
                             {
                                IsDisplayForSubmit=false;
                                IsDisplayForComfirmDate=false;    
                                IsDisplayForSave=false;     
                             }/*
                             if(kamConfirmAsmDateList!=NULL && kamConfirmAsmDateList.size()>0)
                             {
                                set<string> kaConfirmedList=new set<string>();//确认过的KA列表
                                 Set<string> kaDataList=new Set<string>();//当前ASM所拥有的KA
                                 for(KAMConfirmASMDate__c kamConfirm:kamConfirmAsmDateList)
                                 {
                                     string[] strList=kamConfirm.KAList__c.split(';');
                                     kaConfirmedList.addALL(strList);
                                 }
                                 for(SelectOption option:ParticipatorKAList)
                                     kaDataList.add(option.getLabel());
                             
                                 IsDisplayForComfirmDate=getCommonMethod.CompareKAList(kaConfirmedList,kaDataList);
                             }
                            if(parDetailArea.Approve_Type__c=='ASM最后确认时间')
                             {
                                 IsDisplayForComfirmDate=false;
                             }*/
                        }
                        else
                        { 
                           if(outletNumber>0){
                              IsDisplayForSubmit=true;
                              IsDisplayForSave=true;
                            }
                              IsDisplayForComfirmDate=false;
                        }
                   }
           }
         //加载占比计算          
         TotalTargetList=new List<DynamicColumnEntry>();
         if(ActivityInfo.Activity_Type__c!='KA' && ActivityInfo.Regional_Activity_Type__c!='竞品活动'){
          //  TotalTargetList=getCommonMethod.getPercentSaleOrRecourceForCity('Region',RecordId);
            TotalTargetList=getCommonMethod.getPercentSaleOrRecourceForCity(ActivityInfo.Activity_Type__c,user.UserRegion__c,RecordId);
        }         
             
          ETResourceDetailList=new List<Resource_Allocation_Detail__c>();
          FGResourceDetailList=new List<Resource_Allocation_Detail__c>();
              
         //ParticipatorKAList=getCommonMethod.ParticipatorKAList(RecordId,false);//取得用户ＫＡ列表
       //  System.assert(false,CityList.size());
          if(CommonAtt.CitySelectedValue==NULL && CityList.size()>0)
             CommonAtt.CitySelectedValue=CommonAtt.AllCityList[0].getValue();
          if(CommonAtt.KASelectedValue==NULL && ParticipatorKAList.size()>0)
              CommonAtt.KASelectedValue=ParticipatorKAList[0].getValue();
              
         LoadParticipatorOutletList();//加载ＫＡ门店列表
         LoadCompetitionList();//加载KA竞品信息
     }
   
     public PageReference OutletQuery()
     {
         LoadParticipatorOutletList();//加载ＫＡ门店列表
        LoadCompetitionList();//加载ET,FG竞品信息
         return null;
     }
     public void LoadParticipatorOutletList()
     {
         String filter=GetSqlFilter(); 
         ETresourceDetailList.clear();
         FGresourceDetailList.clear();
   //      ETfilter+=' and Resource_Allocation__r.Resource_Type__c=\'ET\' and Participator_Detail_Outlet__r.Participator_Area__r.Type__c=\'ET\'';
         List<Resource_Allocation_Detail__c> resourceDetailList=new List<Resource_Allocation_Detail__c>();
         resourceDetailList=getCommonMethod.LoadOutletList(filter,RecordId);//加载ET门店列表
         for(Resource_Allocation_Detail__c rad:resourceDetailList){
        // System.assert(false,rad);
           if(rad.Resource_Allocation__r.Resource_Type__c=='ET')
               ETresourceDetailList.add(rad);
           else if(rad.Resource_Allocation__r.Resource_Type__c=='FG')
              FGresourceDetailList.add(rad);
         }
         ColumnETList=getCommonMethod.GetDynamicETColumnList(ETresourceDetailList);//获取ET列名
         ETDynamicColumnEntryList=getCommonMethod.GetDynamicEntryList(ETresourceDetailList);//获取ET门店列表
         if(ActivityInfo.Activity_Type__c!='Region(剩余资源)'){
              ColumnFGList=getCommonMethod.GetDynamicETColumnList(FGresourceDetailList);//获取FG列名
              FGDynamicColumnEntryList=getCommonMethod.GetDynamicEntryList(FGresourceDetailList);//获取FG门店列表  
           }         
     }
     
     public List<AllocationResourceEntry> ETAndFGResourceList(string typeStr)
     {  
           String filter=GetSqlFilter();  
          List<AllocationResourceEntry> option=new List<AllocationResourceEntry>();
           if(typeStr=='ET')
           {
               //KA是同一个城市同一个KA之间进行调整
               filter+='and Resource_Allocation__r.Resource_Type__c=\'ET\'';
                         //+ ' and Participator_Detail_Outlet__r.OutletResourceType__c=\''+typeStr+'\'';
             }
             else
             {
                     filter+='and Resource_Allocation__r.Resource_Type__c=\'FG\'';
             }   
            if(SubordinateUserId!=NULL && SubordinateUserId.length()>0)
                filter+=' and Participator_Detail_Outlet__r.OwnerId in ('+SubordinateUserId+')';
        //    filter+=' and Participator_Detail_Outlet__r.Activity_Stage__c=\'SRTM已提交\'';
             return getCommonMethod.LoadResourceAllcationListByOutlet(filter,RecordId);
     }
     //生成OT的竞品费用信息
   /*  public void GenerateCompetition()
     {
         List<Activity_Competition_Brand_Detail__c> cometitionList=new  List<Activity_Competition_Brand_Detail__c>();
         
           //检查OT是否有生成竞品费用信息,按区域生成,ASM只能看到自己有权限看的数据
        Integer countNumber=[select Count() from Activity_Competition_Brand_Detail__c 
                                                    where ActivityId__c=:RecordId and Activity_Competition_Brand_Information__r.Type__c='OT'];
        if(countNumber==0)
        {
            List<Participator_Detail_Outlet__c> parDetailList=[select id,OwnerId from Participator_Detail_Outlet__c
                                                                                          where  ActivityId__c=:RecordId and OutletResourceType__c='OT'];
                                                                                               
             for(Activity_Competition_Brand_Information__c activity:[select id from Activity_Competition_Brand_Information__c
                                                                                                    where Activity_Information__c=:RecordId and Type__c='OT'])
             {
                  for(Participator_Detail_Outlet__c par:parDetailList)
                  {
                      cometitionList.add(new Activity_Competition_Brand_Detail__c(Activity_Competition_Brand_Information__c=activity.Id,
                                                                                                                     Participator_Detail_Outlet__c=par.Id,
                                                                                                                     OwnerId=par.OwnerId));
                  }
             }    
             insert  cometitionList;                                                                   
        }
     }*/
     //加载竞品信息
     public void LoadCompetitionList()
     {
      String filter=GetSqlFilter();
         String sql='select OutletName__c,SubmitASM__c,OutletId__c,Promotion_Type__c,Cost_Type__c,Competition_Cost_Source__c,'+
                            ' Money__c,Start_Date__c,End_Date__c,Display_Type__c from Activity_Competition_Brand_Detail__c'+
                            '  where ActivityId__c=\''+RecordId +'\''
                            +filter;
              //             throw new CustomException (sql);  
             ETCompetitionList=Database.Query(sql);
         
     }
     
     public String GetSqlFilter()
     {
          if(CommonAtt.CitySelectedValue==NULL)
             CommonAtt.CitySelectedValue=CommonAtt.AllCityList[0].getValue();
          if(CommonAtt.KASelectedValue==NULL)
              CommonAtt.KASelectedValue=CommonAtt.OutletKAList[0].getValue();
                //and Participator_Detail_Outlet__r.Participator_Area__r.KA_Status__c=\'KAM已提交\'
        String filter=' and Participator_Detail_Outlet__r.City__c=\''+CommonAtt.CitySelectedValue+'\'';
        if(OutletName!=NULL)
            filter+=' and Participator_Detail_Outlet__r.Outlet_Name__r.Name like \'%'+OutletName+'%\'';
        if(ActivityInfo.Activity_Type__c=='KA'){
            if(CommonAtt.OutletTypeSelectedValue!='' && CommonAtt.OutletTypeSelectedValue!=NULL)
                 filter+=' and Participator_Detail_Outlet__r.OutletType__c=\''+CommonAtt.OutletTypeSelectedValue+'\'';
            if(CommonAtt.ChannelSelectedValue!='' && CommonAtt.ChannelSelectedValue!=NULL)
                 filter+=' and Participator_Detail_Outlet__r.Channel__c=\''+CommonAtt.ChannelSelectedValue+'\'';
            filter+=' and Participator_Detail_Outlet__r.KA__c=\''+CommonAtt.KASelectedValue+'\'';
        }
                     //  filter+=' and Participator_Detail_Outlet__r.OutletResourceType__c=\'OT\'';
            //  filter+=' and Participator_Detail_Outlet__r.Activity_Stage__c=\'SRTM已提交\'';
           if(SubordinateUserId!=NULL && SubordinateUserId.length()>0)
                filter+=' and Participator_Detail_Outlet__r.OwnerId in ('+SubordinateUserId+')';
         return filter;
     }
    
     public PageReference SubmitActivity()
     {
       //  IsDisplayForSubmit=false;
       //  IsDisplayForSave=false;
         boolean flag=true;
         flag=CheckCreetOutletResource('Region',ETDynamicColumnEntryList,flag,ActivityInfo);
         flag=CheckCreetOutletResource('Region',FGDynamicColumnEntryList,flag,ActivityInfo);
         if(!flag)
               throw new CustomException('非NC门店和关闭门店分配的资源必须为零');
               
         CheckOutletResource();

        // if(parDetailArea==NULL)
        // {
             Participator_Detail_Area__c approvalDetailArea=new Participator_Detail_Area__c();
             approvalDetailArea.Name=ActivityInfo.Name;
             approvalDetailArea.Activity_Information__c=RecordId;
             approvalDetailArea.Approve_Type__c='ASM已提交';
             approvalDetailArea.Submit_ASM__c=UserInfo.getUserId();
             approvalDetailArea.Outlet_Amount__c=outletNumber;//门店总数
            
             insert approvalDetailArea;
      //    }
     //     else
     //     {
    //          parDetailArea.Outlet_Amount__c=outletNumber;
    //          parDetailArea.Approve_Type__c='ASM已提交';
           //   parDetailArea.Approve_Description__c='';
    //          update parDetailArea;
      //    }
        
               SaveData(approvalDetailArea.Id);
               updateAllRadAndCom(approvalDetailArea.Id);
         
         //提交审批
         if(ActivityInfo.Activity_Type__c!='KA'){
          Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();   
          req1.setObjectId(approvalDetailArea.Id);
          Approval.ProcessResult result = Approval.process(req1);
          approvalDetailArea.ApprovalId__c=(String)result.getNewWorkitemIds().get(0);
          update approvalDetailArea;
          }
          PageReference pr=new PageReference('/'+RecordId );
          pr.setRedirect(true);
          return pr;
     }
     public PageReference SaveActivity()
     {
         boolean flag=true;
        flag=CheckCreetOutletResource('Region',ETDynamicColumnEntryList,flag,ActivityInfo);
         flag=CheckCreetOutletResource('Region',FGDynamicColumnEntryList,flag,ActivityInfo);
         if(!flag)
               throw new CustomException('非NC门店和关闭门店分配的资源必须为零');
               
         CheckOutletResource();
        SaveData(null);
         PageReference pr=new PageReference('/'+RecordId );
         pr.setRedirect(true);
         return pr;
     }
      //提交时将ParticipatorDetailArea赋给所有资源明细和竞品明细
     public void updateAllRadAndCom(String parId){
        //更改所以分配明细和竞品明细的关连
            List<Resource_Allocation_Detail__c>  radList=[select id,Participator_Detail_Area__c from Resource_Allocation_Detail__c where ActivityId__c=:RecordId  and Participator_Detail_Outlet__r.OwnerId in:SubordinateUserIdList];            
             for(Resource_Allocation_Detail__c rad:radList)
             {
               //  System.assert(false,rad);
                 rad.Participator_Detail_Area__c=parId;
             }
             update radList;  
             List<Activity_Competition_Brand_Detail__c>  acbdList=[select id,SubmitASM__c from Activity_Competition_Brand_Detail__c where ActivityId__c=:RecordId and OwnerId in:SubordinateUserIdList];     
             for(Activity_Competition_Brand_Detail__c acbd:acbdList)
             {
                 acbd.SubmitASM__c=parId;
             }
             update acbdList;
     }
     public void SaveData(String parId)
     {
             //保存竞品信息
         //    if(parId!=NULL)
        //     {   
                 for(Activity_Competition_Brand_Detail__c acbd:ETCompetitionList)
                 {
                   if(acbd.Money__c!=NULL){
                     if(acbd.Start_Date__c==NULL || acbd.End_Date__c==NULL)
                         throw new CustomException('竞品设置了金额，则开始时间、结束时间为必填项');         
                   }         
                 }
             
       //      }
             //保存调整好的门店分配数
             getCommonMethod.SaveAdjustmentResourceForOulet(ETDynamicColumnEntryList,ETResourceDetailList,parId);//保存FG
             if(ActivityInfo.Activity_Type__c!='Region(剩余资源)')
                getCommonMethod.SaveAdjustmentResourceForOulet(FGDynamicColumnEntryList,FGResourceDetailList,parId);//保存ET
             update ETCompetitionList;

             //一旦点击保存之前审批的信息失效
             if(parDetailArea!=NULL)
             {
                 parDetailArea.IsActive__c=false;
                 update parDetailArea;
             }
     }
     public PageReference ConfirmDate()
     {
      //   update parDetailArea;
      //   update ETCompetitionList;
         
          //KA活动，当ASM第一次确认时间后发送邮件给KAM，让KA确认时间，       
  /*     if(activityType=='KA' && parDetailArea.Approve_Type__c=='ASM第一次确认时间'){
         User u=getCommonMethod.getUserInfo();
         String BodyString=u.Name+'已调整了【'+ActivityInfo.Name+'】活动,请登陆凤凰系统进行确认时间,谢谢！';
         String subject='确认【'+ActivityInfo.Name+'】活动时间';
         EmailHander.SendMail(BodyString,getCommonMethod.getUserEmailAddress('KAM',RecordId),subject,'',null);
        }*/
         PageReference pr=new PageReference('/apex/ASMConfirmDatePage?id='+RecordId );
          pr.setRedirect(true);
          return pr;
     }
   //刷新最新的从非NC转NC的门店
     public PageReference RefreshOutlet()
     {
      //   getCommonMethod.RefreshOutlet(RecordId,ActivityInfo.Activity_Start_Date__c,ActivityInfo.Activity_Type__c,ActivityInfo.IsNcOrNotForOT__c,ActivityInfo.IsNcOrNotForKA__c);
         if(ActivityInfo.Activity_Type__c=='KA')
             getCommonMethod.CreateResourceAllocationDetailsForNewOutlet('KA',RecordId);
         else
             getCommonMethod.RefreshOutlet(RecordId,ActivityInfo.Activity_Start_Date__c,'Region',false,false,true);
         return null;
     }
        //判断当前
     public boolean CheckCreetOutletResource(String typeStr,List<DynamicColumnEntry> DynamicColumnEntryList,boolean flag,Activity_Information__c ActivityInfo){
      //
      boolean IsNc=false;
      if(typeStr=='KA'){
        if(ActivityInfo.IsNcOrNotForKA__c==true)
          IsNc=true;
      }
       if(typeStr=='OT'){
        if(ActivityInfo.IsNcOrNotForOT__c==true)
          IsNc=true;
      }
      if(typeStr=='Region'){
        if(ActivityInfo.IsNcOrNotForRegion__c==true)
          IsNc=true;
      }
      if(IsNc){
         for(DynamicColumnEntry d:DynamicColumnEntryList){
           if(d.WithoutNCOrNot==false){
              if(d.Resource1>0){
                 flag=false;
                 break;
              }
              if(d.Resource2>0){
                flag=false;
                break;
              }
               if(d.Resource3>0){
                flag=false;
                break;
                }
               if(d.Resource4>0){
                flag=false;
                break;
                }
               if(d.Resource5>0){
                flag=false;
                break;
                }
           }
         }
      }
      
        for(DynamicColumnEntry d:DynamicColumnEntryList){
        if(d.OutletStatus=='关闭'){
          if(d.Resource1>0){
            flag=false;
            break;
          }
           if(d.Resource2>0){
            flag=false;
            break;
          }
           if(d.Resource3>0){
            flag=false;
            break;
          }
           if(d.Resource4>0){
            flag=false;
            break;
          }
           if(d.Resource5>0){
            flag=false;
            break;
          }
          
        }
       }
       return flag;
     }
     //判断从NC转到非NC时资源必须为零,关闭门店资源也必须为零 
     public void CheckOutletResource()
     {
         string filter='';
         String pFilter='';
         string sql='select id from Resource_Allocation_Detail__c where ActivityId__c=:RecordId and Resource_Quantity_Number__c>0 and Participator_Detail_Outlet__r.Outlet_Name__r.With_NC_or_not__c=false';
         if(ActivityInfo.IsNcOrNotForKA__c)//判断从NC转到非NC时资源必须为零,关闭门店资源也必须为零 
             filter=' ReourceOutletType__c=\'KA\'';
         if(ActivityInfo.IsNcOrNotForOT__c)
         {
             if(filter!='')
                 filter+=' or ReourceOutletType__c=\'OT\'';
             else
                 filter+='  ReourceOutletType__c=\'OT\'';
         }   
           
         if(pFilter!='')
             pFilter+=' or Participator_Detail_Outlet__r.OutletResourceType__c=\'关闭\'';
         else
             pFilter+='  Participator_Detail_Outlet__r.OutletResourceType__c=\'关闭\'';
           
         if(pFilter!='')  
               sql+=' and ('+pFilter+')';
         if(filter!='')
             sql+=' and ('+filter+')';
       //   System.assert(false,sql);   
         List<Resource_Allocation_Detail__c> radList=Database.query(sql);  
         if(radList!=NULL && radList.size()>0)  
              throw new CustomException('非NC门店和关闭门店分配的资源必须为零');
     }
}